package com.drug.service.impl;

import com.drug.domain.Hospital;
import com.drug.domain.PurchaseOrder;
import com.drug.domain.Result;
import com.drug.mapper.PurchaseOrderMapper;
import com.drug.service.PurchaseOrderService;
import com.drug.util.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService {

    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Override
    public Result selectPurchaseOrder(Map map, Integer page) throws ParseException {
        //使用分页插件
        PageHelper.startPage(page,5);
        Result result = new Result();
        //采购单
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        //医院
        Hospital hospital = new Hospital();
        String poAuditResult = (String) map.get("poAuditResult");//审核结果
        String poAuditOpinion = (String) map.get("poAuditOpinion");//审核意见
        String hName = map.get("hName").toString();//医院名称//
        if(hName==null||hName.equals("")){
            hName=null;
        }else{
            hospital.sethName(hName);
        }
        String poNumber = (String) map.get("poNumber");//采购单编号//
        if(poNumber==null||poNumber.equals("")){
            poNumber = null;
        }else{
            purchaseOrder.setPoNumber(poNumber);
        }
        String poName = (String) map.get("poName");//采购单名称//
        if(poName==null||poName.equals("")){
            poName = null;
        }else{
            purchaseOrder.setPoName(poName);
        }
        Date poStateTime = (Date) map.get("poStateTime");
        Date poEndTime = (Date) map.get("poEndTime");//结束时间
        Date poCreateTime = (Date) map.get("poCreateTime");//建单时间
        Date poSubmissionTime = (Date) map.get("poSubmissionTime");//提交时间//
        //采购时间
        String leftTime=null;
        if (leftTime==null||leftTime.equals("")){
        }else {
            leftTime = (String) map.get("leftTime");
        }
        //采购时间
        String rightTime=null;
        if (rightTime==null||rightTime.equals("")){
        }else {
            rightTime = (String) map.get("rightTime");
        }
        Date poAuditTime = (Date) map.get("poAuditTime");//审核时间
        String poState = (String) map.get("poState");//采购单状态//
        if (poState==null||poState.equals("")){
            poState=null;
        }else {
            purchaseOrder.setPoState(poState);
        }
        purchaseOrder.setPoAuditResult(poAuditResult);
        purchaseOrder.setPoAuditOpinion(poAuditOpinion);

        purchaseOrder.setPoStateTime(poStateTime);
        purchaseOrder.setPoEndTime(poEndTime);
        purchaseOrder.setPoCreateTime(poCreateTime);
        purchaseOrder.setPoSubmissionTime(poSubmissionTime);
        purchaseOrder.setPoAuditTime(poAuditTime);

        purchaseOrder.setHospital(hospital);
        List<PurchaseOrder> purchaseOrders = purchaseOrderMapper.selectPurchaseOrder(purchaseOrder,leftTime,rightTime);
        PageInfo<PurchaseOrder> pageInfo = new PageInfo(purchaseOrders);
        result.setData(pageInfo);
        return result;
    }

    /**
     * 修改(提交审核结果)
     * @param purchaseOrder
     * @return
     */
    @Override
    public Result updateSubmit(PurchaseOrder purchaseOrder) {
        Result result = new Result();
        int i = purchaseOrderMapper.updateSubmit(purchaseOrder);
        if(i == 1){
            result.setMsg("修改成功");
        }else{
            result.setMsg("修改失败");
        }
        return result;
    }
}
